package com.ml.service.impl;

import com.ml.dao.BaseDao;
import com.ml.model.KqData;
import com.ml.service.KqDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service()
public class KqDataServiceImpl implements KqDataService {
    @Autowired
    private BaseDao<KqData> basedao;


    @Override
    public KqData get(Integer id) {
        return basedao.get(KqData.class, id);
    }

    @Override
    public void save(KqData model) {
        basedao.save(model);
    }

    @Override
    public void update(KqData model) {
        basedao.update(model);
    }

    @Override
    public void delete(KqData model) {

    }

    /**
     * 根据 时间段 获取 考勤数据
     *
     * @param startDate 2019-07-17
     * @param endDate   2019-07-18
     */
    public List<KqData> getListByDates(String startDate, String endDate) {
        return basedao.find(" from KqData WHERE signDate BETWEEN ? and  ?  ", new Object[]{startDate, endDate});
    }

    /**
     * 根据 日期  获取某天 考勤数据
     *
     * @param date 某天 2019-07-17
     */
    public List<KqData> getListByDate(String date) {
        return basedao.find(" from KqData WHERE signDate = ?  order by signtime asc  ", new Object[]{date});
    }

    /**
     * 根据 日期  获取某天 考勤数据
     * @param date 某天 2019-07-17
     */
    public List<KqData> getListByDate(String date, Integer locationid){
        return basedao.find(" from KqData WHERE signDate = ? and locationid=?  order by signtime asc ", new Object[]{date,locationid});
    }

    /**
     * 根据用户ID 和时间段 获取 考勤数据
     *
     * @param userId
     * @param startDate 2019-07-17
     * @param endDate   2019-07-18
     */
    public List<KqData> getListByUserIdDates(Integer userId, String startDate, String endDate) {
        return basedao.find(" from KqData WHERE userId=? and ( signDate BETWEEN ? and  ? ) ", new Object[]{userId, startDate, endDate});
    }

    /**
     * 根据用户ID 和时间  获取 考勤数据
     *
     * @param userId
     * @param date   2019-07-17
     */
    public List<KqData> getListByUserIdDate(Integer userId, String date) {
        return basedao.find(" from KqData WHERE userId=? and   signDate= ? order by signTime asc ", new Object[]{userId, date});
    }


    /**
     * 获取某人上午第一次打卡，或 下午最后一次打卡
     */
    public KqData getKqDataAmOrPm(Integer userId, String date, String type) {
        KqData kqData = null;
        String hql = "";
        List<KqData> list = new ArrayList<>();
        if (type.equals("am")) {
            hql = " from KqData WHERE userId=? and signDate= ? and ( signTime between '00:00:00' and '12:30:00' )  ";
            list = basedao.find(hql, new Object[]{userId, date});
            if (list != null && list.size() > 0) {
                kqData = list.get(0);
            }
        } else if (type.equals("pm")) {
            hql = " from KqData WHERE userId=? and signDate= ? and ( signTime between '12:30:00' and '23:59:00' ) ";
            list = basedao.find(hql, new Object[]{userId, date});
            if (list != null && list.size() > 0) {
                kqData = list.get(list.size());
            }
        }
        return kqData;
    }


}
